package com.mlm.mlmapp.activity;

import java.util.ArrayList;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Receiver;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.flyco.tablayout.view.CollectionDresserCardFragment;
import com.flyco.tablayout.view.CollectionStyleCardFragment;
import com.flyco.tablayout.view.OrderFragment;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.interfaces.Prefs_;

/**
 * Activity which displays a login screen to the user, offering registration as
 * well.
 */
@EActivity(R.layout.collections)
public class CollectionsActivity extends BaseActivity implements HTTPCons {
	private static final int TYPE_DRESSER = 0;
	private static final int TYPE_STYLE = 1;
	public static final String COLLECTION_REFRESH="collectionrefresh";
	@Pref
	Prefs_ pref;
	@ColorRes
	int red;
	@ColorRes
	int lightgreytext;
	@ColorRes
	int blacktext;
	public String currentJson = "";
	@ViewById
	TextView tv_no_title;
	@ViewById
	ViewPager vp;
	@ViewById
	RadioGroup top_group;
	private ArrayList<Fragment> mFragments = new ArrayList<>();

	@AfterViews
	void init() {
		mFragments.add(new CollectionDresserCardFragment( pref.loginID().get()));
		mFragments.add(new CollectionStyleCardFragment(pref.loginID().get()));
		vp.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		vp.addOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				RadioButton rb=(RadioButton)top_group.getChildAt(arg0);
				rb.setChecked(true);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		vp.setOffscreenPageLimit(2);
		vp.setKeepScreenOn(true);
	}

	@CheckedChange
	void btn_style(boolean isChecked){
		if(isChecked){
			vp.setCurrentItem(TYPE_STYLE);
		}
	}
	@CheckedChange
	void btn_dresser(boolean isChecked){
		if(isChecked){
			vp.setCurrentItem(TYPE_DRESSER);
		}
	}


	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		this.setResult(RESULT_CANCELED, intent);
		this.finish();
	}
	@Receiver(actions = CollectionsActivity.COLLECTION_REFRESH, registerAt = Receiver.RegisterAt.OnCreateOnDestroy)
	protected void onOrderRefresh(Intent intent) {
		if (intent.getStringExtra("refreshflag")!=null&&"0".equals(intent.getStringExtra("refreshflag"))){
			if(mFragments.size()>1){
				((CollectionDresserCardFragment)mFragments.get(0)).refresh();
				((CollectionStyleCardFragment)mFragments.get(1)).refresh();
			}

		}


	}
	@Click
	void btn_back() {
		onBackPressed();
	}

	private class MyPagerAdapter extends FragmentPagerAdapter {
		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return "";
		}

		@Override
		public Fragment getItem(int position) {
			return mFragments.get(position);
		}
	}

}
